//
//  F2DAppDelegate.m
//  spriteTest
//
//  Created by Silvina Roldan on 31/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "F2DAppDelegate.h"
#import "F2DSpriteBackground.h"
#import "F2DAtlas.h"
#import "RootViewController.h"


@implementation F2DAppDelegate

@synthesize window = _window;
@synthesize glView = _glView;
@synthesize rootViewController = _rootViewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    
    self.window.rootViewController = _rootViewController;
    
    _glView = (EAGLView*)_rootViewController.view;
    
    [self.window makeKeyAndVisible];
    
    _dl = [CADisplayLink displayLinkWithTarget:self selector:@selector(gameLoop)];
    
    _dl.frameInterval = 1;
    
    [_dl addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    
    //load textures    
    
    F2DTexture* tex = [[F2DTexture alloc] initWithContentsFromFile:[[NSBundle mainBundle] pathForResource:@"pong" ofType:@"png"] filter:GL_LINEAR];
    
    F2DAtlas* atlas = [[F2DAtlas alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pong" ofType:@"plist"] texture:tex];
    
    //create gameobjects
    _gameObjects = [[NSMutableArray alloc] init];    
    
    F2DSpriteBackground *sprite= [[F2DSpriteBackground alloc] init];  
    sprite.atlas = atlas;
    sprite.SpriteName = @"GameBackground";
    sprite.transform.scale = CGSizeMake(1, 1);
    [_gameObjects addObject:sprite];
    
    //    F2DSprite *sprite2= [[F2DSprite alloc] init];  
    //    sprite2.atlas = atlas;
    //    sprite2.SpriteName = @"GameBall";
    //    sprite2.transform.scale = CGSizeMake(1, 1);
    //    [_gameObjects addObject:sprite2];
    //    
    //    F2DSprite *sprite3= [[F2DSprite alloc] init];  
    //    sprite3.atlas = atlas;
    //    sprite3.SpriteName = @"GamePaddle";
    //    sprite3.transform.scale = CGSizeMake(1, 1);
    //    [_gameObjects addObject:sprite3];
    
    return YES;
}


- (void) render 
{
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    
    glViewport(0, 0, _glView.framebufferWidth, _glView.framebufferHeight);
    
    glMatrixMode(GL_PROJECTION);
    //    glOrthof(-_glView.framebufferWidth / 2 , _glView.framebufferWidth / 2, -_glView.framebufferHeight / 2, _glView.framebufferHeight / 2, -1, 1);
    
    glLoadIdentity();
    
    //    float oSize = _glView.framebufferHeight * 0.5f;
    float oH = 480;
    float oW = 320;
    
    //glOrthof(-oW , oW, -oH, oH, -1, 1);
    glOrthof(-1 , 1, -1, 1, -1, 1);
    
    //    glMatrixMode(GL_TEXTURE);
    //    glLoadIdentity();
    //    
    glTranslatef(0.5,0.5,0.0);
    glRotatef(90,0.0,0.0,1.0);
    glTranslatef(-0.5,-0.5,0.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
    for (id sprite in _gameObjects) {
        if ([sprite respondsToSelector:@selector(draw)]) {
            [sprite draw];
        }
    }
    
    
    [_glView.context presentRenderbuffer:GL_RENDERBUFFER_OES];
}


#define MAX_FRAME_TIME 0.25f

- (void) gameLoop
{
    static double time = 0.0f;
    static double accumulator = 0.0f;
    static double lastFrameTime = 0.0f;
    static double dt = (1.0f/60.0f);
    
    double newTime = CACurrentMediaTime();
    
    if (lastFrameTime == 0.0f)
        lastFrameTime = newTime;
    
    double frameTime = (newTime - lastFrameTime);
    
    if (frameTime > MAX_FRAME_TIME)
        frameTime = MAX_FRAME_TIME;
    
    lastFrameTime = newTime;
    
    accumulator += frameTime;
    
    while (accumulator >= dt)
    {
        //update game
        time += dt;
        accumulator -= dt;
        
        //     [sprite update:dt];
        
        
    }
    
    //render game
    [self render];
}
@end
